package list_AI;

class Player {
    String name;
    int index;
    Player next;

    Player(String name, int index) {
        this.name = name;
        this.index = index;
        this.next = null;
    }

    @Override
    public String toString() {
        return "Player{" +
                "name='" + name + '\'' +
                ", index=" + index +
                '}';
    }
}

public class List_AI {
	    public static void main(String[] args) {
	        int n = 7; // 总人数
	        int k = 3; // 每数到k的人出列
	        int startIndex = 0; // 从第0个开始计数

	        // 创建链表并初始化玩家
	        Player head = new Player("Player1", 1);
	        Player current = head;
	        for (int i = 2; i <= n; i++) {
	            current.next = new Player("Player" + i, i);
	            current = current.next;
	        }
	        current.next = head; // 形成循环链表

	        // 找到起始位置的前一个节点
	        Player previous = null;
	        current = head;
	        for (int i = 0; i < startIndex; i++) {
	            previous = current;
	            current = current.next;
	        }

	        // 开始报数并移除玩家
	        while (current.next != current) {
	            for (int i = 1; i < k; i++) {
	                previous = current;
	                current = current.next;
	            }
	            System.out.println("Removing: " + current);
	            previous.next = current.next; // 移除当前玩家
	            current = current.next; // 继续下一个玩家
	        }

	        System.out.println("Last remaining player: " + current);
	    }
	}

